﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MySite.Model;
using MySite.DAL;

namespace MySite.BLL
{
    public class Vote
    {
        #region 更新微博信息
        /// <summary>
        /// 更新微博信息(插入和更新)
        /// </summary>
        /// <param name="vinfo"></param>
        /// <returns></returns>
        public ReturnMessage UpdateVote(VoteInfo vinfo)
        {
            ReturnMessage rm = new ReturnMessage(true);
            VoteDAL sdal = new VoteDAL();
            rm = sdal.Exist(vinfo);
            if (rm.IsSuccessed)
            {
                vinfo.PID = Convert.ToInt32(rm.ReturnData["PID"]);
                rm = sdal.Update(vinfo);
            }
            else
            {
                rm = sdal.Add(vinfo);
            }

            return rm;
        }
        #endregion

        #region 更新微博信息
        /// <summary>
        /// 更新微博信息
        /// </summary>
        /// <param name="siList"></param>
        /// <returns></returns>
        public ReturnMessage UpdateVote(IList<VoteInfo> siList)
        {
            ReturnMessage rm = new ReturnMessage(true);

            try
            {
                foreach (var item in siList)
                {
                    rm = UpdateVote(item);

                    if (!rm.IsSuccessed)
                    {
                        return rm;
                    }
                }
            }
            catch (Exception e)
            {
                rm.IsSuccessed = false;
                rm.Exception = e;
            }

            return rm;
        }
        #endregion

        #region 查询
        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="where"></param>
        /// <returns></returns>
        public IList<VoteInfo> Query(string where)
        {
            return new VoteDAL().Query(where);
        }

        public IList<VoteInfo> Query(string where, int top)
        {
            return new VoteDAL().Query(where, top);
        }

        public IList<VoteInfo> Query(string where, string orderBy, int pageIndex, int pageSize, ref int pageCount)
        {
            return new VoteDAL().Query(where, pageSize, pageIndex, orderBy, ref pageCount);
        }

        public VoteInfo TakeOneByID(int id)
        {
            VoteInfo vi =  new VoteDAL().Query(string.Format("ID={0}",id), 1).FirstOrDefault();

            if (vi != null)
            {
                vi.VoteItems = new VoteItem().Query(string.Format("VoteID={0}",id));
            }

            return vi;
        }
        #endregion
    }
}
